package mumu.android.lib.view;

import java.util.ArrayList;
import java.util.List;

import mumu.android.R;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.AttributeSet;
import android.view.Gravity;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.LinearLayout;

public class DotView extends LinearLayout {

	private Context c;
	public int ChooseCount = 0;
	List<ImageView> imageViews = new ArrayList<ImageView>();
	private Integer[] img = { R.drawable.dot_1, R.drawable.dot_2 };

	public DotView(Context context) {
		super(context);
		setOrientation(LinearLayout.HORIZONTAL);
	}

	public DotView(Context context, AttributeSet attrs) {
		super(context, attrs);
		this.c = context;
		setOrientation(LinearLayout.HORIZONTAL);
	}

	public void initView(final int count, final int index, Boolean change) {

		if (change) {
			DotView.this.removeAllViews();
			Bitmap imgbit = BitmapFactory.decodeResource(c.getResources(), img[0]);
			// 创建ImageView并设置属性
			for (int i = 0; i < count; i++) {
				final ImageView imageView = new ImageView(c);
				imageView.setTag(i);
				if (index == i)
					imageView.setBackgroundResource(img[1]);
				else {
					imageView.setBackgroundResource(img[0]);
				}
				LayoutParams params = new LayoutParams(imgbit.getWidth(), imgbit.getHeight());
				params.setMargins(4, 2, 4, 1);
				params.gravity = Gravity.CENTER_HORIZONTAL;// 居中
				imageView.setLayoutParams(params);
				imageView.setScaleType(ScaleType.CENTER_INSIDE);
				imageView.setTag(i);
				imageViews.add(imageView);
				addView(imageView);
			}
		} else {
			for (int i = 0; i < count; i++) {
				ImageView imageView = (ImageView) this.findViewWithTag(i);
				if (imageView != null) {
					if (i == index)
						imageView.setBackgroundResource(img[1]);
					else {
						imageView.setBackgroundResource(img[0]);
					}
				}
			}
		}

	}
}
